package com.example.evensystem.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.example.evensystem.model.database.SystemDb
import com.example.evensystem.model.entity.Account
import com.example.evensystem.model.entity.DailyWork
import com.example.evensystem.model.entity.MonthPlan
import com.example.evensystem.model.entity.MonthlyWork
import com.example.evensystem.model.entity.Worker
import com.example.evensystem.utils.DateUtils
import java.util.Date

class MainViewModel (private val systemDb: SystemDb): ViewModel(
) {

    fun findWorker(id:String): Worker? {
        val wlist=systemDb.getWorkerDao().findWorker(id)
        if(wlist.isEmpty()){
            return null
        }else{
            return wlist[0]
        }
    }
    fun insertWorker(worker: Worker){
        systemDb.getWorkerDao().insert(worker)
    }
    fun deleteWorker(worker: Worker){
        systemDb.getWorkerDao().delete(worker)
    }

    fun findAllWorker():List<Worker>{
        return systemDb.getWorkerDao().findAllWorker()
    }

    fun findDW(id: String,date: String):DailyWork?{
        val dwList=systemDb.getDailyWorkDao().find(id,date)
        if (dwList.isEmpty()){
            return null
        }else{
            return dwList[0]
        }
    }
    fun insertDW(dailyWork: DailyWork){
        systemDb.getDailyWorkDao().insert(dailyWork)
    }

    fun findMW(id: String,month: String):MonthlyWork?{
        val mwList=systemDb.getMonthlyWorkDao().find(month,id)
        if(mwList.isEmpty()){
            return null
        }else{
            return mwList[0]
        }
    }
    fun insertMW(monthlyWork: MonthlyWork){
        systemDb.getMonthlyWorkDao().insert(monthlyWork)
    }

    fun findAccount(id: String):Account? {
        val aList=systemDb.getAccountDao().find(id)
        if(aList.isEmpty()){
            return null
        }else{
            return aList[0]
        }
    }
    fun insertAccount(account: Account){
        systemDb.getAccountDao().insert(account)
    }
    fun deleteAccount(account: Account){
        systemDb.getAccountDao().delete(account)
    }
    fun findAllAccount():List<Account>{
        return systemDb.getAccountDao().findAll()
    }

    fun findMonthPlan(month: String):MonthPlan?{
        val mpList=systemDb.getMonthPlanDao().find(month)
        if(mpList.isEmpty()){
            return null
        }else{
            return mpList[0]
        }
    }
    fun insertMonthPlan(monthPlan: MonthPlan){
        systemDb.getMonthPlanDao().insert(monthPlan)
    }
    //用于登陆的工号
    private val _id_login = MutableLiveData<String>()
    val id_login: LiveData<String>
        get() = _id_login

    fun editId(id: String) {
        _id_login.value = id
    }

    //用于登陆的密码
    private val _passwd_login = MutableLiveData<String>()
    val passwd_login: LiveData<String>
        get() = _passwd_login

    fun editPasswd(passwd: String) {
        _passwd_login.value = passwd
    }


    //考勤状态
    private val _checkStatus = MutableLiveData<CheckStatus>(CheckStatus.NOT_ARRIVED)
    val checkStatus: LiveData<CheckStatus>
        get() = _checkStatus

    fun editCheckStatus(status: CheckStatus) {
        _checkStatus.value = status
    }

    //上班时间
    private val _arrivedTime=MutableLiveData<String>()
    val arrivedTime:LiveData<String>
        get() = _arrivedTime

    fun editArrivedNowTime(){
        _arrivedTime.value= DateUtils.formTime(Date())
    }
    fun editArrivedTime(time :String){
        _arrivedTime.value=time
    }

    //下班时间
    private val _leftTime=MutableLiveData<String>()
    val leftTime:LiveData<String>
        get() = _leftTime

    fun editLeftNowTime(){
        _leftTime.value= DateUtils.formTime(Date())
    }
    fun editLeftTime(time :String){
        _leftTime.value=time
    }

    //用于查询的id
    private val _idFind=MutableLiveData<String>()
    val idFind:LiveData<String>
        get()=_idFind
    fun editIdFind(id:String){
        _idFind.value=id
    }

    ////用于查询日总结的日期
    private val _dateDW=MutableLiveData<String>()
    val dateDW:LiveData<String>
        get() = _dateDW

    fun editdateDW(date:String){
        _dateDW.value=date
    }

    //用于查询日总结的上班时间
    private val _arrivedTimeDW=MutableLiveData<String>()
    val arrivedTimeDW:LiveData<String>
        get()=_arrivedTimeDW
    fun editArrivedTimeDW(time:String){
        _arrivedTimeDW.value=time
    }

    //用于查询日总结的下班时间
    private val _leftTimeDW=MutableLiveData<String>()
    val leftTimeDW:LiveData<String>
        get()=_leftTimeDW
    fun editleftTimeDW(time:String){
        _leftTimeDW.value=time
    }


    //用于查询月总结的月份
    private val _monthMW=MutableLiveData<String>()
    val monthMW:LiveData<String>
        get() = _monthMW
    fun editMonthMW(month:String){
        _monthMW.value=month
    }

    //用于查询月总结的迟到次数
    private val _lateTimeMW=MutableLiveData<Int>()
    val lateTimeMW:LiveData<Int>
        get()=_lateTimeMW
    fun editLateTimeMW(time:Int){
        _lateTimeMW.value=time
    }
    //用于查询月总结的早退次数
    private val _earlyTimeMW=MutableLiveData<Int>()
    val earlyTimeMW:LiveData<Int>
        get()=_earlyTimeMW
    fun editEarlyTimeMW(time:Int){
        _earlyTimeMW.value=time
    }
    //用于查询月总结的已工作天数
    private val _workedDaysMW=MutableLiveData<Int>()
    val workedDaysMW:LiveData<Int>
        get()=_workedDaysMW
    fun editworkedDaysMW(days:Int){
        _workedDaysMW.value=days
    }
    //用于查询月总结的剩余天数
    private val _leftDaysMW=MutableLiveData<Int>()
    val leftDaysMW:LiveData<Int>
        get()=_leftDaysMW
    fun editleftDaysMW(days:Int){
        _leftDaysMW.value=days
    }
    //用于查询月总结的剩余时间
//    private val _leftHoursMW=MutableLiveData<Int>()
//    val leftHoursMW:LiveData<Int>
//        get()=_leftHoursMW
//    fun editleftHoursMW(days:Int){
//        _leftHoursMW.value=days
//    }

    //用于工作安排的月份

    //用于工作安排的上班天数
    //用于工作安排的上班时间
    //用于工作安排的下班时间

}